import { get } from 'lodash';
import { recommendSong } from '../../api/song';

const state = {
  recommendMusic: [],
  test: 'hello test',
  inputModel: 'hello',
};

const getters = {
  myTestGetters: (state) => state.test,
  myInputModel: (state) => state.inputModel,
  getTestLength: (state, getters, rootState, rootGetters) => getters.myTestGetters.length,
  getMyTest: (state) => (condition) => {
    if (condition) {
      return state.test;
    } return '啥都没有';
  },
  getTopRecommendMusic: (state) => (count) => {
    if (state.recommendMusic.length < count) {
      return state.recommendMusic;
    }
    const offset = Math.random() * (state.recommendMusic.length - count);
    return state.recommendMusic.slice(offset, count + offset);
  },
};

const mutations = {
  addString(state) {
    state.test += 'add success';
  },
  addMyString(state, { text }) {
    state.test += text;
  },
  addMyObj(state, { name }) {
    state.test += name;
  },
  addRecommendMusic(state, payload) {
    if (payload) {
      state.recommendMusic = payload;
    }
  },
  addInputModel(state, payload) {
    state.inputModel = payload;
  },
};

const actions = {
  addStringActions({ commit, rootState }) {
    // console.log(get(rootState, 'request'))
    // setTimeout(() => {
    //   console.log('log in')
    //   commit('addString')
    // }, 5000)
  },
  storeGetRecommendSong({ commit }) {
    recommendSong().then((data) => {
      commit('addRecommendMusic', get(data, 'data.data'));
    });
  },
};


export default {
  state,
  getters,
  mutations,
  actions,
};
